﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackState : FSMState
{
    [Tooltip("是否在射击模式")]
    public bool isShoting = false;
    public float distance_Gun = 0.7f;
    public float shotV = 2f;
    public float minShotTime = 1f;
    public float maxShotTime = 5f;
    public Shot shotType = Shot.ENEMY_RAY;
    public AttackState()
    {
        id = StateID.Attack;
    }

    public override void DoBeforeEnter()
    {
        base.DoBeforeEnter();
        isShoting = true;//开启射击模式开关
        StartCoroutine(RandomShot());//启动射击模式
    }

    public override bool Reason()
    {
        bool result = base.Reason();
        if(result)isShoting = false;//如果转换条件成立,则关闭射击模式开关
        return result;
    }


    IEnumerator RandomShot()
    {
        while (isShoting)
        {

            yield return new WaitForSeconds(UnityEngine.Random.Range(minShotTime, maxShotTime));
            GameObject py = GameManager.Instance.Sui;
            if (py == null) break;
            Vector3 tagert = py.transform.position;
            Vector3 charp = tagert - transform.position;
            Vector3 shotVelocity = charp.normalized * shotV;
            Vector3 startPos = transform.position + distance_Gun*charp.normalized;
            Shots.Shot(shotVelocity, startPos, (int)shotType);//射击的类型
        }
    }
}
